<?php
namespace Core\Forms;

use Phalcon\Forms\ElementInterface;
use Phalcon\Di;

/**
 * Class Init
 * @package Core\Forms
 */
class Init
{
    /**
     * @var array
     */
    public static $element = array();
    /**
     * @var array
     */
    public static $validate = array();
    /**
     * @var array
     */
    public static $field = array();

    /**
     *
     */
    public static function init()
    {
        if (!self::$element) {
            self::initElement();
        }
        if (!self::$validate) {
            self::initValidate();
        }
    }

    /**
     * @return array
     */
    public static function initElement()
    {
        $di = Di::getDefault();
        $default = array(
            'Group' => '\Core\Forms\Init\Element::group',
            'GroupTabs' => '\Core\Forms\Init\Element::groupTabs',
            'Radio' => '\Core\Forms\Init\Element::radio',
            'Autoinput' => '\Core\Forms\Init\Element::Autoinput',
            'Chosen' => '\Core\Forms\Init\Element::Chosen',
            'Radios' => '\Core\Forms\Init\Element::radios',
            'Select' => '\Core\Forms\Init\Element::select',
            'Selects' => '\Core\Forms\Init\Element::selects',
            'Password' => '\Core\Forms\Init\Element::password',
            'Email' => '\Core\Forms\Init\Element::email',
            'Checkbox' => '\Core\Forms\Init\Element::check',
            'Text' => '\Core\Forms\Init\Element::text',
            'Url' => '\Core\Forms\Init\Element::url',
            'ValidateCode' => '\Core\Forms\Init\Element::validateCode',
            'Tags' => '\Core\Forms\Init\Element::tags',
            'Date' => '\Core\Forms\Init\Element::date',
            'Textarea' => '\Core\Forms\Init\Element::textarea',
            'Submit' => '\Core\Forms\Init\Element::submit',
            'Numeric' => '\Core\Forms\Init\Element::numeric',
            'Hidden' => '\Core\Forms\Init\Element::hidden',
            'Kvgroup' => '\Core\Forms\Init\Element::kvgroup',
        );
        $config = $di->getShared('config')->cache('formElement');
        self::$element = array_merge($default, $config);
        return self::$element;

    }

    /**
     * @return array
     */
    public static function initValidate()
    {
        $di = Di::getDefault();
        $default = array(
            'PresenceOf' => '\Core\Forms\Init\Validation::presenceOf',
            'Identical' => '\Core\Forms\Init\Validation::identical',
            'Function' => '\Core\Forms\Init\Validation::functions',
            'Email' => '\Core\Forms\Init\Validation::email',
            'ExclusionIn' => '\Core\Forms\Init\Validation::exclusionIn',
            'InclusionIn' => '\Core\Forms\Init\Validation::inclusionIn',
            'Regex' => '\Core\Forms\Init\Validation::regex',
            'StringLength' => '\Core\Forms\Init\Validation::stringLength',
            'Between' => '\Core\Forms\Init\Validation::between',
            'Confirmation' => '\Core\Forms\Init\Validation::confirmation',
            'Alnum' => '\Core\Forms\Init\Validation::alnum',
            'CreditCard' => '\Core\Forms\Init\Validation::creditCard',
            'Digit' => '\Core\Forms\Init\Validation::digit',
            'Date' => '\Core\Forms\Init\Validation::date',
            'Url' => '\Core\Forms\Init\Validation::url',
            'File' => '\Core\Forms\Init\Validation::file',
        );
        $config = $di->getShared('config')->cache('formValidate');
        self::$validate = array_merge($default, $config);
        return self::$validate;
    }

    /**
     * @param $t
     * @param $element
     * @return bool | \Phalcon\Forms\ElementInterface
     */
    public static function callElement(&$t, &$element)
    {
        if (isset(self::$element[$element['widget']])) {
            return self::$element[$element['widget']]($t, $element);
        } else {
            return false;
        }
    }

    /**
     * @param $t
     * @param $validate
     * @return bool
     */
    public static function callValidate(ElementInterface &$element, $validate)
    {
        if (isset($validate['v']) && isset(self::$validate[$validate['v']])) {
            return self::$validate[$validate['v']]($element, $validate);
        } else {
            return false;
        }
    }
}
